<?php
class lienhe_model extends CI_Model
{
	public function __construct()
	{
		parent::__construct();
	} 
	// Category                 
	function get_list($num,$offset)
	{
		$this->db->where('bl_active !=',-1);
		$query = $this->db->get('lienhe');
		return $query->result();
	}
	
	function count_list()
	{
		$this->db->where('bl_active !=',-1);
		$query = $this->db->get('lienhe');
		return $query->num_rows();
	}
	
	function set_view($id)
	{
		$data = array(
			'bl_active' => 0
		);
		$this->db->where('id',$id);
		$this->db->update('lienhe',$data) ;
	}
	
	function delete($id)
	{
		$data = array(
			'bl_active' 	=> -1
		);
		$this->db->where('id',$id);
		$this->db->update('lienhe',$data) ;
	}
	
	function getlienhe($id=0, $num=15, $start=0)
	{ 
		if(!empty($id)) $this->db->where('id',$id);
		$query = $this->db->get('lienhe', $num, $start);
		return $query->row();
	}
	
	function save()
	{
		$id = (int)$this->uri->segment(3);
		$subject_vn = $this->input->post('subject_vn');
		$address_vn = $this->input->post('address_vn');
		$ks_image  = $this->input->post('ks_image');
		$subcontent_vn = $this->input->post('subcontent_vn');
		$content_vn  = $this->input->post('content_vn');
		$bl_active  = $this->input->post('bl_active');
		
		$data = array(
			'subject_vn' => $subject_vn,
			'alias_vn' => strtolower(stringchange($subject_vn)),
			'address_vn' => $address_vn,
			'subcontent_vn' => $subcontent_vn,
			'content_vn' => $content_vn,
			'ks_image' => $ks_image,
			'bl_active' 	=> $bl_active
		);
		if($id != 0)
		{
		  $this->db->where('id',$id);
		  if($this->db->update('lienhe',$data)){
			  return true;
		  }else{
			  return false;
		  }
		}else{
		
		  if($this->db->insert('lienhe',$data)){
			  return true;
		  }else{
			  return false;
		  }
		}          
	}
	
	function getItemCat()
	{
		$this->db->where('App',0);
		$catid = $this->uri->segment(3);
		$this->db->where('CatID',$catid);
		$query = $this->db->get('contentcat');
		return $query->row();
	}
	
	function get_Parent($CatID)
	{
		$this->db->where('App',0);
		$this->db->where('ks_image',$CatID);
		return $this->db->get('contentcat')->result();
	}
	
	function delcat($id)
	{
		$this->db->where('App',0);
		$this->db->where('CatID',$id);
		if($this->db->delete('contentcat')){
		  return true;
		}else{
		  return false;
		}
	}
	
	// Begin List lienhe
	function getNumlienhe()
	{      
		//$this->db->where('App',0);
		$query = $this->db->get('lienhe');
		return $query->num_rows();
	}
	
	function getListCat(){
	return $this->db->get('contentcat')->result();
	}   
	//Save lienhe
	function savelienhe()
	{
		$id = (int)$this->uri->segment(3);
		$data = array(
			'chude_vn' => (string)$this->input->post('chude_vn'),
			'content_vn_vn' => strtolower(stringchange($this->input->post('content_vn_vn'))),
			'image' => (string)$this->input->post('image'),
			'trichdoan_vn' => (string)$this->input->post('trichdoan_vn'),
			'noidung_vn' => (string)$this->input->post('noidung_vn'),
			'bl_active' => (int)$this->input->post('bl_active'),
			'lastedit' => time()
		);
		if($id!=0){
		  $this->db->where('id',$id);
		  if($this->db->update('lienhe',$data)){
			  return true;
		  }else{
			  return false;
		  }
		}else{
		  if($this->db->insert('lienhe',$data)){
			  return true;
		  }else{
			  return false;
		  }
		}
	}
	
	function dellienhe($id)
	{
		$this->db->select('id, imglienhe');
		$this->db->where('App',0);
		$this->db->where('id',$id);
		$check = $this->db->get('content')->row();
		if($check)
		{
		  if(file_exists('../uploads/lienhe/'.$check->imglienhe)){
			  unlink('../uploads/lienhe/'.$check->imglienhe);
		  }
		  $this->db->where('id',$id);
		  if($this->db->delete('content')){
			  return true;
		  }else{
			  return false;
		  }
		}else{
		  return false;
		}
	}
	
	//Update lienhe
	function getItemlienhe()
	{
		$id = (int)$this->uri->segment(3);
		$this->db->where('id',$id);
		$query = $this->db->get('lienhe');
		return $query->row();
	}
}
?>
